﻿using System;
using System.IO;
using System.Net;
using System.Security;
using System.Security.Permissions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Configuration;
using System.Globalization;

namespace Flitter
{
    public partial class App : System.Windows.Application
    {
        /// <summary>
        /// これがエントラントポイント。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void OnStartup(Object sender, StartupEventArgs e)
        {
            string[] args = e.Args;
            if (args.Length > 0)
            {
                // Get the 2 character command line argument
                //これでいいのかという気もしないでもないが。まあいいことにする。
                string arg = args[0].ToLower(CultureInfo.InvariantCulture).Trim().Substring(0, 2);
                switch (arg)
                {
                    case "/c":
                        // Show the options dialog
                        SettingWindow settings = new SettingWindow();
                        settings.Show();
                        break;
                    case "/p":
                        // Don't do anything for preview
                        Application.Current.Shutdown();
                        break;
                    case "/s":
                        // Show screensaver form
                        ShowScreensaver();
                        break;
                    default:
                        Application.Current.Shutdown();
                        break;
                }
            }
            else
            {
                // If no arguments were passed in, show the screensaver
                ShowScreensaver();
            }



        }

        /// <summary>
        /// スクリーンセーバーとして起動。
        /// </summary>
        void ShowScreensaver()
        {
            //creates window on primary screen
            Window1 primaryWindow = new Window1();
            primaryWindow.WindowStartupLocation = WindowStartupLocation.Manual;
            System.Drawing.Rectangle location = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
            primaryWindow.WindowState = WindowState.Maximized;

            //set background apprpropriate to bounds on the main grid
            Grid g = (Grid)primaryWindow.Content;

            //creates window on other screens
            foreach (System.Windows.Forms.Screen screen in System.Windows.Forms.Screen.AllScreens)
            {
                if (screen == System.Windows.Forms.Screen.PrimaryScreen)
                    continue;

                Window1 window = new Window1();
                window.WindowStartupLocation = WindowStartupLocation.Manual;
                location = screen.Bounds;

                //set the background for any other screens
                Grid g2 = (Grid)window.Content;

                //covers entire monitor
                window.Left = location.X - 7;
                window.Top = location.Y - 7;
                window.Width = location.Width + 14;
                window.Height = location.Height + 14;

            }

            //show non-primary screen windows
            foreach (Window window in System.Windows.Application.Current.Windows)
            {
                if (window != primaryWindow)
                    window.Show();

            }

            ///shows primary screen window last
            primaryWindow.Show();
        }


    }
}
